<?php
class Person_model extends CI_Model
{

	public function __construct()
	{
		$this->load->database();
	}

	public function get ($id)
	{
		$sql = "select id, surname, first_names, bday_18, first_seen_date " .
			   "from person " .
			   "where id = ?" ;

		$query = $this->db->query($sql, array($id));

		// single result
		return $query->row();
	}
	
    public function get_addresses ($id)
    {
        $sql = "select pd.name, er.compile_date, a.id address_id, a.address1, a.address2, a.address3, a.address4, a.address5, a.postcode " .
               "from electoral_roll_entry ere, polling_district pd, electoral_roll er, address a " .
               "where ere.person = ? " .
               "and ere.electoral_roll = er.id " .
               "and er.polling_district = pd.id " .
               "and ere.address = a.id " .
               "order by ere.id desc";
        
        $query = $this->db->query($sql, array($id));

        return $query->result();
    }
    
/*    
    public function get_addresses ($id)
    {
        $sql = "select w.display_name, er.compile_date, a.id address_id, a.address1, a.address2, a.address3, a.address4, a.address5, a.postcode " .
               "from electoral_roll_entry ere, ward w, electoral_roll er, address a " .
               "where ere.person = ? " .
               "and ere.electoral_roll = er.id " .
               "and er.ward = w.id " .
               "and ere.address = a.id " .
               "order by ere.id desc";
        
        $query = $this->db->query($sql, array($id));

        return $query->result();
    }
*/    
    public function get_voting_record ($id)
    {
        $sql = "select e.election_date, e.election_type, v.voted " .
               "from election e, voting_info v " .
               "where v.person = ? " .
               "and v.election = e.id " .
               "order by e.election_date desc";
        
        $query = $this->db->query($sql, array($id));

        return $query->result();
    }
    
    public function get_contacts ($id)
    {
        $sql = "select contact_date, contacted_by, party, notes, rank " .
               "from contact " .
               "where person = ? " .
               "order by contact_date desc";
        
        $query = $this->db->query($sql, array($id));

        return $query->result();
    }
    
    public function get_aliases ($id)
    {
        $sql = "select p.id, first_names, surname, confirmed, notes " .
               "from person_match pm, person p " .
               "where (person1 = ? and p.id = person2) " .
               "or (person2 = ? and p.id = person1)";
        
        $query = $this->db->query($sql, array($id, $id));

        return $query->result();
    }
    
    public function add_contact_details ($personId,
                                         $contact_date,
                                         $contacted_by,
                                         $party,
                                         $rank,
                                         $notes)
    {
        $sql = "insert into contact (person, contact_date, contacted_by, party, rank, notes, updated_by, updated_on) " .
               "values (?, ?, ?, ?, ?, ?, ?, ?)";
        
        $this->load->helper('user');
        
        $updated_by = get_current_username();
        $now = strftime ("%Y-%m-%d", time());
        
        $query = $this->db->query($sql, array($personId,
   	                                          $contact_date,
 	                                          $contacted_by,
 	                                          $party,
 	                                          $rank,
 	                                          $notes,
 	                                          $updated_by,
 	                                          $now));
                                         
        return $this->db->affected_rows();                                 
    }
}
?>
